package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.stat.StatMchDayBO;
import com.ymk.bo.stat.StatMchMonthBO;
import com.ymk.bo.stat.StatMchYearBO;
import com.ymk.core.code.cmd.StatMchCmdCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.service.IStatMchYearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StatMchYearPostCmd extends ACmd {

    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        StatMchYearBO reqData = super.getData(sign, StatMchYearBO.class);

        StatMchYearBO statMchYearBO = new StatMchYearBO();
        statMchYearBO.setMchId(reqData.getMchId());
        statMchYearBO.setYear(reqData.getYear());


        StatMchMonthBO statMchMonthBO = new StatMchMonthBO();
        statMchMonthBO.setMchId(reqData.getMchId());
        statMchMonthBO.setYear(reqData.getYear());

        CmdSign cmdSign = CmdSign.Builder.newBuild(sign)
                .setSource(statMchMonthBO)
                .setCmdCode(StatMchCmdCode.STAT_MCH_MONTH_LIST)
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .build();

        // 获取当年数据
        List<StatMchMonthBO> statMchMonthBOList = this.scheduler.schedule(request, cmdSign, StatMchMonthBO.class, true);

        // 获取新增会员数量
        Integer memberCount = statMchMonthBOList.stream().collect(Collectors.summingInt(StatMchMonthBO::getMemberQuantity));
        statMchYearBO.setMemberQuantity(memberCount);
        // 获取新增订单
        Integer ordersCount = statMchMonthBOList.stream().collect(Collectors.summingInt(StatMchMonthBO::getOrdersQuantity));
        statMchYearBO.setOrdersQuantity(ordersCount);
        // 获取新增销量
        Map<Integer, BigDecimal> salesMap = statMchMonthBOList.stream().collect(Collectors.groupingBy(StatMchMonthBO::getId
                , Collectors.mapping(StatMchMonthBO::getSales, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
        BigDecimal sales = new BigDecimal(0);
        for (Integer i : salesMap.keySet()){
            sales =  sales.add(salesMap.get(i));
        }
        statMchYearBO.setSales(sales);
        // 获取新增销售金额
        Map<Integer, BigDecimal> payQuantityMap = statMchMonthBOList.stream().collect(Collectors.groupingBy(StatMchMonthBO::getId
                , Collectors.mapping(StatMchMonthBO::getPayQuantity, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
        BigDecimal payQuantity = new BigDecimal(0);
        for (Integer i : payQuantityMap.keySet()){
            payQuantity =  payQuantity.add(payQuantityMap.get(i));
        }
        statMchYearBO.setPayQuantity(payQuantity);


        this.StatMchYearService.post(statMchYearBO);

        return respSign;
    }

    @Override
    public String getCmdCode() {
        return StatMchCmdCode.STAT_MCH_YEAR_POST;
    }

    @Autowired
    private IStatMchYearService StatMchYearService;
}